using System;
using UnityEngine;

[RequireComponent(typeof(tk2dSprite))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshFilter))]
[ExecuteInEditMode]
public class tk2dSpriteExpand : MonoBehaviour
{
	private void Awake()
	{
		this.sprite = base.gameObject.GetComponent<tk2dBaseSprite>();
	}

	private void Start()
	{
		this.Init();
	}

	private void Init()
	{
		this._mesh = base.gameObject.GetComponent<MeshFilter>().sharedMesh;
		this._vertices = new Vector3[this._mesh.vertices.Length];
		this._mesh.vertices.CopyTo(this._vertices, 0);
		this._uvs = new Vector2[this._mesh.uv.Length];
		this._mesh.uv.CopyTo(this._uvs, 0);
		this._progressVertices = new Vector3[16];
		this._progressUvs = new Vector2[16];
		this._progressTriangles = new int[54];
		this._progressColors = new Color[16];
		tk2dBaseSprite component = base.gameObject.GetComponent<tk2dBaseSprite>();
		this._flipTextureY = (component.Collection.spriteDefinitions[component.spriteId].flipped != tk2dSpriteDefinition.FlipMode.None);
		this.Commit();
	}

	public void Commit()
	{
		if (null == this._mesh)
		{
			return;
		}
		Vector3 localScale = base.transform.localScale;
		if (localScale.x < 0f)
		{
			localScale.x *= -1f;
		}
		if (localScale.y < 0f)
		{
			localScale.y *= -1f;
		}
		this._progressVertices[0] = this._vertices[0];
		this._progressVertices[1] = (this._vertices[1] - this._vertices[0]) * this.marginRate.xMin / localScale.x + this._vertices[0];
		this._progressVertices[2] = (this._vertices[0] - this._vertices[1]) * (1f - this.marginRate.xMax) / localScale.x + this._vertices[1];
		this._progressVertices[3] = this._vertices[1];
		this._progressVertices[4] = (this._vertices[2] - this._vertices[0]) * this.marginRate.yMin / localScale.y + this._vertices[0];
		this._progressVertices[8] = (this._vertices[0] - this._vertices[2]) * (1f - this.marginRate.yMax) / localScale.y + this._vertices[2];
		this._progressVertices[12] = this._vertices[2];
		this._progressVertices[13] = (this._vertices[3] - this._vertices[2]) * this.marginRate.xMin / localScale.x + this._vertices[2];
		this._progressVertices[14] = (this._vertices[2] - this._vertices[3]) * (1f - this.marginRate.xMax) / localScale.x + this._vertices[3];
		this._progressVertices[15] = this._vertices[3];
		this._progressVertices[7] = (this._vertices[3] - this._vertices[1]) * this.marginRate.yMin / localScale.y + this._vertices[1];
		this._progressVertices[11] = (this._vertices[1] - this._vertices[3]) * (1f - this.marginRate.yMax) / localScale.y + this._vertices[3];
		this._progressVertices[5] = new Vector3(this._progressVertices[1].x, this._progressVertices[4].y, 0f);
		this._progressVertices[6] = new Vector3(this._progressVertices[2].x, this._progressVertices[4].y, 0f);
		this._progressVertices[9] = new Vector3(this._progressVertices[1].x, this._progressVertices[8].y, 0f);
		this._progressVertices[10] = new Vector3(this._progressVertices[2].x, this._progressVertices[8].y, 0f);
		if (!this._flipTextureY)
		{
			Vector2 a = this._uvs[1] - this._uvs[0];
			this._uvs[0] = this._uvs[0] + a * this.uvRate.xMin;
			this._uvs[1] = this._uvs[0] + a * this.uvRate.width;
			Vector2 a2 = this._uvs[3] - this._uvs[2];
			this._uvs[2] = this._uvs[2] + a2 * this.uvRate.xMin;
			this._uvs[3] = this._uvs[2] + a2 * this.uvRate.width;
			Vector2 a3 = this._uvs[2] - this._uvs[0];
			this._uvs[0] = this._uvs[0] + a3 * this.uvRate.yMin;
			this._uvs[2] = this._uvs[0] + a3 * this.uvRate.height;
			Vector2 a4 = this._uvs[3] - this._uvs[1];
			this._uvs[1] = this._uvs[1] + a4 * this.uvRate.yMin;
			this._uvs[3] = this._uvs[1] + a4 * this.uvRate.height;
		}
		else
		{
			Vector2 a5 = this._uvs[2] - this._uvs[0];
			this._uvs[0] = this._uvs[0] + a5 * this.uvRate.yMin;
			this._uvs[2] = this._uvs[0] + a5 * this.uvRate.height;
			Vector2 a6 = this._uvs[3] - this._uvs[1];
			this._uvs[1] = this._uvs[1] + a6 * this.uvRate.yMin;
			this._uvs[3] = this._uvs[1] + a6 * this.uvRate.height;
			Vector2 a7 = this._uvs[1] - this._uvs[0];
			this._uvs[0] = this._uvs[0] + a7 * this.uvRate.xMin;
			this._uvs[1] = this._uvs[0] + a7 * this.uvRate.width;
			Vector2 a8 = this._uvs[3] - this._uvs[2];
			this._uvs[2] = this._uvs[2] + a8 * this.uvRate.xMin;
			this._uvs[3] = this._uvs[2] + a8 * this.uvRate.width;
		}
		if (!this._flipTextureY)
		{
			this._progressUvs[0] = this._uvs[0];
			this._progressUvs[1] = (this._uvs[1] - this._uvs[0]) * this.marginRate.xMin + this._uvs[0];
			this._progressUvs[2] = (this._uvs[0] - this._uvs[1]) * (1f - this.marginRate.xMax) + this._uvs[1];
			this._progressUvs[3] = this._uvs[1];
			this._progressUvs[4] = (this._uvs[2] - this._uvs[0]) * this.marginRate.yMin + this._uvs[0];
			this._progressUvs[8] = (this._uvs[0] - this._uvs[2]) * (1f - this.marginRate.yMax) + this._uvs[2];
			this._progressUvs[12] = this._uvs[2];
			this._progressUvs[13] = (this._uvs[3] - this._uvs[2]) * this.marginRate.xMin + this._uvs[2];
			this._progressUvs[14] = (this._uvs[2] - this._uvs[3]) * (1f - this.marginRate.xMax) + this._uvs[3];
			this._progressUvs[15] = this._uvs[3];
			this._progressUvs[7] = (this._uvs[3] - this._uvs[1]) * this.marginRate.yMin + this._uvs[1];
			this._progressUvs[11] = (this._uvs[1] - this._uvs[3]) * (1f - this.marginRate.yMax) + this._uvs[3];
			this._progressUvs[5] = new Vector2(this._progressUvs[1].x, this._progressUvs[4].y);
			this._progressUvs[6] = new Vector2(this._progressUvs[2].x, this._progressUvs[4].y);
			this._progressUvs[9] = new Vector2(this._progressUvs[1].x, this._progressUvs[8].y);
			this._progressUvs[10] = new Vector2(this._progressUvs[2].x, this._progressUvs[8].y);
		}
		else
		{
			this._progressUvs[0] = this._uvs[0];
			this._progressUvs[4] = (this._uvs[2] - this._uvs[0]) * this.marginRate.yMin + this._uvs[0];
			this._progressUvs[8] = (this._uvs[0] - this._uvs[2]) * (1f - this.marginRate.yMax) + this._uvs[2];
			this._progressUvs[12] = this._uvs[2];
			this._progressUvs[1] = (this._uvs[1] - this._uvs[0]) * this.marginRate.xMin + this._uvs[0];
			this._progressUvs[2] = (this._uvs[0] - this._uvs[1]) * (1f - this.marginRate.xMax) + this._uvs[1];
			this._progressUvs[3] = this._uvs[1];
			this._progressUvs[7] = (this._uvs[3] - this._uvs[1]) * this.marginRate.yMin + this._uvs[1];
			this._progressUvs[11] = (this._uvs[1] - this._uvs[3]) * (1f - this.marginRate.yMax) + this._uvs[3];
			this._progressUvs[15] = this._uvs[3];
			this._progressUvs[13] = (this._uvs[3] - this._uvs[2]) * this.marginRate.xMin + this._uvs[2];
			this._progressUvs[14] = (this._uvs[2] - this._uvs[3]) * (1f - this.marginRate.xMax) + this._uvs[3];
			this._progressUvs[5] = new Vector2(this._progressUvs[4].x, this._progressUvs[1].y);
			this._progressUvs[6] = new Vector2(this._progressUvs[4].x, this._progressUvs[2].y);
			this._progressUvs[9] = new Vector2(this._progressUvs[8].x, this._progressUvs[1].y);
			this._progressUvs[10] = new Vector2(this._progressUvs[8].x, this._progressUvs[2].y);
		}
		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 3; j++)
			{
				this._progressTriangles[18 * j + 6 * i] = 4 * j + i;
				this._progressTriangles[18 * j + 6 * i + 1] = 4 * (j + 1) + i;
				this._progressTriangles[18 * j + 6 * i + 2] = 4 * (j + 1) + i + 1;
				this._progressTriangles[18 * j + 6 * i + 3] = 4 * j + i;
				this._progressTriangles[18 * j + 6 * i + 4] = 4 * (j + 1) + i + 1;
				this._progressTriangles[18 * j + 6 * i + 5] = 4 * j + i + 1;
			}
		}
		if (null != this.sprite && this._progressColors != null)
		{
			for (int k = 0; k < 16; k++)
			{
				this._progressColors[k] = this.sprite.color;
			}
		}
		this._mesh.vertices = this._progressVertices;
		this._mesh.uv = this._progressUvs;
		this._mesh.colors = this._progressColors;
		this._mesh.triangles = this._progressTriangles;
		this._mesh.RecalculateBounds();
		this._mesh.RecalculateNormals();
	}

	private Mesh _mesh;

	private Vector3[] _vertices;

	private Vector2[] _uvs;

	private bool _flipTextureY;

	private Vector3[] _progressVertices;

	private Vector2[] _progressUvs;

	private int[] _progressTriangles;

	private Color[] _progressColors;

	[NonSerialized]
	public tk2dBaseSprite sprite;

	public Rect marginRate = new Rect(0.1f, 0.1f, 0.8f, 0.8f);

	public Rect uvRate = new Rect(0f, 0f, 1f, 1f);
}
